/*
  * Purpose：    获取表头分组数据
  * Params：     参数
    columns [数组] 针对73接口返回的数据进行处理
  * */
export const obtainHeaderGroupingData = (columns) => {
  let childrenType = {};
  let filterColumns = [];
  let lastChildrenIndex;
  for (let i = 0; i < columns.length; i++) {
    let currentItem = columns[i];
    const linkMethod = currentItem?.linkMethod;
    const linkService = currentItem?.linkService;
    if (linkMethod === 'children' && linkService) {
      if (!childrenType[linkService]) {
        let currentItemRender = currentItem?.render;
        let currentHeaderCell = currentItem?.lastHeaderCell;
        currentItem.render = (text, record, index) => { // 给子表头数据添加分割线
          return {
            props: {
              className: 'common-table-children-first-td'
            },
            children: currentItemRender ? currentItemRender(text, record, index) : text
          }
        };
        currentItem.onHeaderCell = () => ({
          className: 'common-table-children-first-td',
          ...(currentHeaderCell ? currentHeaderCell() : {})
        })
        childrenType[linkService] = [currentItem];
        filterColumns.push({
          title: linkService,
          childrenFlag: 'Y'
        });
      } else {
        childrenType[linkService].push(currentItem);
      }
    } else {
      filterColumns.push(currentItem)
    }
  }
  for (let j = 0; j < filterColumns.length; j++) {
    let currentIndex = j;
    if (filterColumns[j].childrenFlag === 'Y') {
      filterColumns[j].children = childrenType[filterColumns[j].title] || [];
      lastChildrenIndex = currentIndex;
    }
  }
  // 在最后一个带有 "children" 的项之后添加 className
  if (lastChildrenIndex !== undefined && filterColumns && Array.isArray(filterColumns) && filterColumns.length - 1 >= lastChildrenIndex + 1) {
    let lastItemRender = filterColumns[lastChildrenIndex + 1]?.render;
    let lastHeaderCell = filterColumns[lastChildrenIndex + 1]?.lastHeaderCell;
    filterColumns[lastChildrenIndex + 1].render = (text, record, index) => { // 给子表头数据添加分割线
      return {
        props: {
          className: 'common-table-children-first-td'
        },
        children: lastItemRender ? lastItemRender(text, record, index) : text
      }
    };
    filterColumns[lastChildrenIndex + 1].onHeaderCell = () => ({
      className: 'common-table-children-first-td',
      ...(lastHeaderCell ? lastHeaderCell() : {})
    })
  }
  return filterColumns;
}

/*
  * Purpose：    获取表头分组数据
  * Params：     参数
    tableData [数组] table列表数据
    selectedRowKeys [数组] table勾选keys
    direction [string] 移动类型【   up - 上移       down - 下移  】
    tableDataDataIndex [string] 返回table列表数据对应接收的字段
    selectedRowKeysDataIndex [string] 返回table选中key数据对应接收的字段
  * */
export const checkAndMoveTheTable = (tableData, selectedRowKeys, direction, tableDataDataIndex = 'tableData', selectedRowKeysDataIndex = 'selectedRowKeys') => {
  if (tableData && Array.isArray(tableData) && selectedRowKeys && Array.isArray(selectedRowKeys)) {
    var newKeys = [];
    // 排序
    selectedRowKeys.sort(function (m, n) {
      return m - n;
    });
    // 根据方向设置起始和结束索引、以及步长
    var start = direction === 'up' ? 0 : selectedRowKeys.length - 1;
    var end = direction === 'up' ? selectedRowKeys.length : -1;
    var step = direction === 'up' ? +1 : -1;
    for (var i = start; i !== end; i += step) {
      if ((direction === 'up' && selectedRowKeys[i] === 0)
        || (direction === 'down' && selectedRowKeys[i] === tableData.length - 1)) {
        newKeys.push(selectedRowKeys[i]);
        continue;
      }
      var curIndex = selectedRowKeys[i];
      var targetIndex = curIndex + (direction === 'up' ? -1 : +1);
      [tableData[curIndex], tableData[targetIndex]] = [tableData[targetIndex], tableData[curIndex]];
      [tableData[curIndex].key, tableData[targetIndex].key] = [tableData[targetIndex].key, tableData[curIndex].key];
      newKeys.push(targetIndex);
    }
  }
  return {
    [tableDataDataIndex]: tableData,
    [selectedRowKeysDataIndex]: newKeys
  };
}